package com.wave.wavesystem.business.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wave.wavesystem.business.mapper.MenuMapper;
import com.wave.wavesystem.business.pojo.entity.MenuEntity;
import com.wave.wavesystem.business.pojo.vo.MenuVO;
import com.wave.wavesystem.business.service.MenuService;
import wave.common.pojo.entity.KV;
import com.wave.wavesystem.common.util.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-01-22
 * @description 菜单表 业务层接口
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuEntity> implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<MenuVO> allMenu(String type) {
        QueryChain<MenuEntity> queryChain = QueryChain.of(MenuEntity.class)
                .eq(MenuEntity::getParent, 0)
                .orderBy(MenuEntity::getOrder, true);
        if (Func.isNotEmpty(type)) {
            queryChain.eq(MenuEntity::getType, type);
        }
        List<MenuEntity> parentMenus = this.list(queryChain);
        if (Func.isEmpty(parentMenus)) return Collections.emptyList();
        return fillMenu(parentMenus);
    }

    @Override
    public List<KV<String, String>> topMenu(String type) {
        QueryChain<MenuEntity> queryChain = QueryChain.of(MenuEntity.class)
                .eq(MenuEntity::getParent, 0)
                .eq(MenuEntity::getEnable, true)
                .orderBy(MenuEntity::getOrder, true);
        if (Func.isNotEmpty(type)) {
            queryChain.eq(MenuEntity::getType, type);
        }
        List<MenuEntity> topMenu = this.list(queryChain);
        if (Func.isEmpty(topMenu)) return Collections.emptyList();
        return topMenu.stream().map(item -> new KV<>(item.getName(), String.valueOf(item.getId()))).toList();
    }

    @Override
    public List<MenuVO> childrenMenu(String parentId) {
        QueryChain<MenuEntity> queryChain = QueryChain.of(MenuEntity.class)
                .eq(MenuEntity::getParent, parentId)
                .eq(MenuEntity::getEnable, true)
                .orderBy(MenuEntity::getOrder, true);
        List<MenuEntity> childrenMenu = this.list(queryChain);
        if (Func.isEmpty(childrenMenu)) return Collections.emptyList();
        return fillMenu(childrenMenu);
    }

    /**
     * 获取全部菜单
     *
     * @param parentMenus 父级菜单
     */
    private List<MenuVO> fillMenu(List<MenuEntity> parentMenus) {
        List<MenuVO> allMenu = new ArrayList<>();
        for (MenuEntity parentMenu : parentMenus) {
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(parentMenu, menuVO);
            allMenu.add(menuVO);
            List<MenuEntity> childrenMenus = this.list(QueryChain.of(MenuEntity.class)
                    .eq(MenuEntity::getParent, parentMenu.getId())
                    .eq(MenuEntity::getEnable, true)
                    .orderBy(MenuEntity::getOrder, true));
            if (Func.isEmpty(childrenMenus)) continue;
            List<MenuVO> childrenMenu = fillMenu(childrenMenus);
            menuVO.setChildren(childrenMenu);
        }
        return allMenu;
    }


}
